app.controller('memberCardCtrl',['$scope','toaster','utilService','memberCardService','$modal',
    function($scope,toaster,utilService,memberCardService,$modal){
    $scope.memberCardVO={};
    $scope.generalMemberCard={};
    $scope.ruleMemberCard={};
    $scope.paymentMemberCard={};

    $scope.query = function(){
        memberCardService.getMemberCard($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.generalMemberCard = result.data[0];
                    $scope.ruleMemberCard = result.data[1];
                    $scope.paymentMemberCard = result.data[2];
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.query();

    //删除会员卡
    $scope.memberCardDel = function(id){
        // query data.
        openModal("确定要删除该会员卡吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"deleteCtrl",
                message:msg,
                data:id
            });
        }
    };

    //获取该商家有规则会员卡的最大值
    $scope.maxCard={
        cardGrade:0
    };
    $scope.queryMaxCard = function() {
        memberCardService.queryMaxCard($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.maxCard = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.queryMaxCard();

}]);

app.controller('deleteCtrl', ['$scope', '$modalInstance','$state','memberCardService','toaster','modalData',
    function($scope, $modalInstance,$state,memberCardService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.cardId = {
            id: modalData.data
        };
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            memberCardService.deleteCard($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.queryMaxCard();
                        $scope.query();
                        break;
                    case 666:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该会员卡！");
                        $scope.isDelModal=true;
                        $scope.queryMaxCard();
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);